package com.xinxin.facelinker.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.ShowBlacklistAdapter;
import com.xinxin.facelinker.domain.ShowBlacklist;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemClickListener;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单列表页面
 */
public class ShowBlacklistActivity extends FragmentActivity implements OnMenuItemClickListener,
        OnMenuItemLongClickListener {
    private ListView lvBlacklist;
    private String my_account_num;
    private String other_account_num;
    HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager=getSupportFragmentManager();
    private DialogFragment mMenuDialogFragment;
    private List<ShowBlacklist> list;
    private ShowBlacklistAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_list);

        lvBlacklist = (ListView) findViewById(R.id.lvBlacklist);
        btnBack=findViewById(R.id.btnBack);
        my_account_num = getIntent().getStringExtra(Config.KEY_MY_ACCOUNT_NUM);
        btnMenu= (ButtonFloat) findViewById(R.id.btnMenu);

        initMenuFragment();

        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentManager.beginTransaction().add(mMenuDialogFragment, "").commit();
            }
        });

        getBlacklist();

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void getBlacklist(){
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_BLACKLIST);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<List<ShowBlacklist>>() {
                }.getType());

                adapter=new ShowBlacklistAdapter(ShowBlacklistActivity.this,list);
                lvBlacklist.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ShowBlacklistActivity.this, R.string.blacklist_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject pals_motion = new MenuObject("朋友圈");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_pals_motion);
        pals_motion.setBitmap(b);

        MenuObject my_motion = new MenuObject("自己动态");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_my_motion);
        my_motion.setBitmap(b);

        MenuObject near_pal = new MenuObject("附近的人");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.drawable.btn_near_pal));
        near_pal.setDrawable(bd);

        MenuObject female_toplist = new MenuObject("女神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_female_toplist);
        female_toplist.setBitmap(b);

        MenuObject male_toplist = new MenuObject("男神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_male_toplist);
        male_toplist.setBitmap(b);

        menuObjects.add(close);
        menuObjects.add(pals_motion);
        menuObjects.add(my_motion);
        menuObjects.add(near_pal);
        menuObjects.add(female_toplist);
        menuObjects.add(male_toplist);

        return menuObjects;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else{
            finish();
        }
    }

    public void onMenuItemClick(View clickedView, int position) {
        if(position==1){
            Intent intent=new Intent(ShowBlacklistActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }else if (position==2){
            Intent intent=new Intent(ShowBlacklistActivity.this,ShowMyMotionActivity.class);
            startActivity(intent);
            finish();

        }else if (position==3){
            Intent intent=new Intent(ShowBlacklistActivity.this,ShowNearUserMotionActivity.class);
            startActivity(intent);
            finish();

        }else if(position==4){
            Intent intent=new Intent(ShowBlacklistActivity.this,ShowFemaleToplistActivity.class);
            startActivity(intent);
            finish();

        }else if(position==5){
            Intent intent=new Intent(ShowBlacklistActivity.this,ShowMaleToplistActivity.class);
            startActivity(intent);
            finish();

        }
    }

    public void onMenuItemLongClick(View clickedView, int position) {
    }
}
